const app = getApp();

Page({
  data: {
    orderList: {},
    status:'',
    page:1,
    countPage:'',
    order_no:true
  },
  onBlur(e) {
    this.setData({
      inputValue: e.detail.value,
    });
  },
  onShareAppMessage() {
    return {
      title: '花礼鲜花玫瑰生日',
      desc: '花礼鲜花玫瑰生日',
      path: 'pages/todos/todos'
    };
  },
  onLoad(r){
    my.setNavigationBar({
      title: '订单列表',
    });
    this.setData({
      status: r.status
    })
    this.orderList();
  },
  onShow(){
    this.orderList();
  },
  onPullDownRefresh() {
    var that = this;
    that.setData({
      page:1
    });
    that.orderList();
    my.stopPullDownRefresh();
  },
  onReachBottom() {
    var that = this;
    if(that.data.countPage>that.data.page){
      that.setData({
        page:that.data.page+1
      });
      that.orderList();
    }
  },
  orderList(){
    var that = this;
    my.getStorage({
      key: 'token',
      success: function(res) {
        var arr = new Object();
        arr['page'] = that.data.page;
        arr['token'] = res.data.token;
        if(that.data.status>0){
          if(that.data.status==1){
            arr['refund_status'] = 1;
          }else{
            arr['order_status'] = that.data.status;
          }
        }
        my.httpRequest({
          url: app.geturl+'/order/list',
          method: 'GET',
          data:arr,
          dataType: 'json',
          success: function(re) {
            console.info(re);
            if(re.data.code==0){
              if(re.data.data.list.length>0){
                that.setData({
                  orderList:re.data.data.list,
                  countPage:re.data.data.pageArr.pageTotal
                });
              }else{
                that.setData({
                  order_no:false
                });
              }
            }else{
              app.memberLogin();
            }
          }
        });
      }
    });
  },
  cancleOrder(e){
    var id = e.target.dataset.id;
    var that = this;
    my.confirm({
      title: '温馨提示',
      content: '您确定要取消订单吗？',
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      success: (result) => {
        if(result.confirm){
          my.getStorage({
            key: 'token',
            success: function(res) {
              my.httpRequest({
                url: app.geturl+'/order/cancel',
                method: 'GET',
                data: {
                  token: res.data.token,
                  order_ids:id
                },
                dataType: 'json',
                success: function(res) {
                  if(res.data.code==0){
                    console.info(res);
                    that.orderList();
                  }else{
                    app.memberLogin();
                  }
                }
              });
            }
          });
        }
      },
    });    
  },
  goOrder(e){
    var id = e.target.dataset.id;
    my.getStorage({
      key: 'token',
      success: function(res) {          
         my.httpRequest({
          url: app.geturl+'/pay/alipay-app',
          method: 'GET',
          data: {
            token:res.data.token,
            order_ids:id,
            order_type:'order'
          },
          dataType: 'json',
          success: function(r) {
            if(r.data.code==0){
              console.info( r.data.data.payStr);
              my.tradePay({
                orderStr: r.data.data.payStr, //完整的支付参数拼接成的字符串，从服务端获取
                success: (rr) => {
                  my.reLaunch({
                    url: '../add-todo/order/'
                  })
                },
                fail: (rr) => {
                    my.reLaunch({
                    url: '../add-todo/order/'
                  })
                }
              });
            }else{
              app.memberLogin();
            }
          }
        }); 
      }
    });
  },
  subOrder(e){
    var that = this;
    var id = e.target.dataset.id;
    my.confirm({
      title: '温馨提示',
      content: '您确定要完成订单吗？',
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      success: (result) => {
        if(result.confirm){
          my.getStorage({
            key: 'token',
            success: function(res) {          
              my.httpRequest({
                url: app.geturl+'/order/confirm',
                method: 'GET',
                data: {
                  token:res.data.token,
                  order_ids:id
                },
                dataType: 'json',
                success: function(r) {
                  if(r.data.code==0){
                    that.orderList();
                  }else{
                    app.memberLogin();
                  }
                }
              }); 
            }
          });
        }
      },
    });
  },
  returnOrder(e){
    var id = e.target.dataset.id;
    my.navigateTo({ url: '../add-todo/order_return?id='+id });
  },
  add() {
    app.todos = app.todos.concat([{
      text: this.data.inputValue,
      compeleted: false,
    }]);

    my.navigateBack();
  },
  handleTap(){
    my.navigateTo({ url: '../todos/todos' });
  },
  orderDetail(e){
    var id = e.target.dataset.id;
    my.navigateTo({ url: '../add-todo/order_detail?id='+id });
  },
});
